/* eslint-disable */
const { resolve } = require('path');
HtmlWebpackPlugin = require('html-webpack-plugin');
MiniCssExtractPlugin = require('mini-css-extract-plugin');
OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');

process.env.NODE_ENV = 'production'; //修改node.js环境变量

const commonCssLoader = [
    {
        loader: MiniCssExtractPlugin.loader,
        options: {
            publicPath: '../',
        },
    },
    'css-loader',
    {
        //需要再package.json中定义browserslist
        loader: 'postcss-loader',
        options: {
            ident: 'postcss',
            plugins: () => [require('postcss-preset-env')()],
        },
    },
];
module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'js/built.[contenthash:10].js',
        path: resolve(__dirname, 'build'),
    },
    module: {
        rules: [
            {
                //需要再package.json中配置eslintConfig -->airbnb
                test: /\.js$/,
                exclude: /node_modules/,
                //优先执行
                enforce: 'pre',
                loader: 'eslint-loader',
                options: {
                    fix: true,
                },
            },
            {
                // 以下loader只会匹配一个
                //注意：不能有两个配置处理同一类文件
                oneOf: [
                    {
                        test: /\.css$/,
                        use: [...commonCssLoader],
                    },
                    {
                        test: /\.less$/,
                        use: [...commonCssLoader, 'less-loader'],
                    },

                    {
                        test: /\.js$/,
                        exclude: /node_modules/,
                        use: [
                            /**
                             * 开启多进程打包
                             * 进程启动时间600ms,进程通信也有开销
                             * 只有工作时间比较长才使用
                             */
                            {
                                loader: 'thread-loader',
                                options: {
                                    workers: 2, //进程2个
                                },
                            },

                            {
                                loader: 'babel-loader',
                                options: {
                                    presets: [
                                        [
                                            '@babel/preset-env',
                                            {
                                                useBuiltIns: 'usage',
                                                corejs: { version: 3 },
                                                //指定兼容性做到哪个浏览器的哪个版本
                                                targets: {
                                                    chrome: 60,
                                                },
                                            },
                                        ],
                                    ],
                                    // 开启babel缓存
                                    // 第二次构建时，会读取之前的缓存
                                    cacheDirectory: true,
                                },
                            },
                        ],
                    },
                    {
                        test: /\.(jpg|png|gif)$/,
                        loader: 'url-loader',
                        options: {
                            limit: 8 * 1024,
                            name: '[hash:10].[ext]',
                            outputPath: 'img',
                            esModule: false,
                        },
                    },
                    {
                        test: /\.html$/,
                        loader: 'html-loader',
                    },
                    {
                        exclude: /\.(js|css|less|html|jpg|png|gif)$/,
                        loader: 'file-loader',
                        options: {
                            outputPath: 'media',
                        },
                    },
                ],
            },
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
            minify: {
                collapseWhitespace: true,
                removeComments: true,
            },
        }),
        new MiniCssExtractPlugin({
            filename: 'css/index.[contenthash:10].css',
        }),
        new OptimizeCssAssetsWebpackPlugin(),
    ],
    mode: 'production',
    devtool: 'source-map',
};
